package edu.uwaterloo.bicycle.framework;

import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.SwingUtilities;

class MoveMouseListener implements MouseListener, MouseMotionListener {
  private final Container target;
  private Point start_drag;
  private Point start_loc;

  public MoveMouseListener(final Container target) {
    this.target = target;
  }

  private Point getScreenLocation(final MouseEvent e) {
    final Point cursor = e.getPoint();
    final Point target_location = this.target.getLocationOnScreen();
    return new Point(target_location.x + cursor.x, target_location.y + cursor.y);
  }

  @Override
  public void mouseClicked(final MouseEvent e) {
  }

  @Override
  public void mouseDragged(final MouseEvent e) {
    final Point current = this.getScreenLocation(e);
    SwingUtilities.getRoot(this.target).setLocation(
        new Point(this.start_loc.x + current.x - this.start_drag.x, this.start_loc.y + current.y - this.start_drag.y));
  }

  @Override
  public void mouseEntered(final MouseEvent e) {
  }

  @Override
  public void mouseExited(final MouseEvent e) {
  }

  @Override
  public void mouseMoved(final MouseEvent e) {
  }

  @Override
  public void mousePressed(final MouseEvent e) {
    this.start_drag = this.getScreenLocation(e);
    this.start_loc = SwingUtilities.getRoot(this.target).getLocation();
  }

  @Override
  public void mouseReleased(final MouseEvent e) {
  }
}
